package fojta;

public class Resistor {
	
	private ResistorPoint[] links = null;
	private byte             used = 0;
	private float      resistance = 1;
	private        int id         = 0;   
	private static int lastId     = 0;
	
	static final int MAX_COUNT = 1000;
	
	// TODO rewrite constant 1000 to dynamic self-enlarged field
	public static Resistor[] field =
		new Resistor[MAX_COUNT];
	
	private static int nextId() {
		return ++lastId;
	}
	
	public float getResistance() {
		return resistance;
	}

	public void setResistance(float resistance) {
		this.resistance = resistance;
	}

	/**
	 * Create not used/connected resistor
	 */
	Resistor() {
		id = nextId();
		field[id] = this;

		links = new ResistorPoint[2];
		links[0] = links[1] = null;
	}
	
	/**
	 * Create resistor connected between a and b {@link ResistorPoint}
	 * @param a
	 * @param b
	 */
	Resistor(ResistorPoint a, ResistorPoint b) {
		id = nextId();
		field[id] = this;

		links = new ResistorPoint[2]; 
		links[0] = a;
		links[1] = b;
		used = 2;
	}
	
	/**
	 * Connect resistor to {@link ResistorPoint} a
	 * @param a
	 * @return
	 */
	public int connect(ResistorPoint a) {
		if( used >= 2 ) {
			return -1;
		}
		links[used++] = a;
		
		return 0;
	}

	/**
	 * return connected {@link ResistorPoint}
	 * @param id
	 * @return
	 */
	ResistorPoint getLink(byte id) {
		if(id > 1) {
			return null;
		}
		return links[id];
	}
	

}
